package com.mytest;
  
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
   
public class BouncingBallView extends View {
   private int xMin = 0;          // This view's bounds
   private int xMax;
   private int yMin = 0;
   private int yMax;
   private int F1=5;
   private int F2=18;
   private int F3=20;
   private int F4=2;
   private int T1=(int)1000/F1;
   private int T2=(int)1000/F2;
   private int T3=(int)1000/F3;
   private int T4=(int)1000/F4;
   private int colorTop = Color.WHITE;
   private int colorLeft = Color.BLACK;
   private int colorRight = Color.WHITE;
   private int colorBottom = Color.BLACK;
   private Rect rT, rL, rR, rB;
   private int i = 0, j;
   
   /*
   private float ballRadius = 80; // Ball's radius
   private float ballX = ballRadius + 20;  // Ball's center (x,y)
   private float ballY = ballRadius + 40;
   private float ballSpeedX = 5;  // Ball's speed (x,y)
   private float ballSpeedY = 3;
   */
   private RectF ballBounds;      // Needed for Canvas.drawOval
   private Paint paint;           // The paint (e.g. style, color) used for drawing
   
   // Constructor
   public BouncingBallView(Context context) {
      super(context);
      ballBounds = new RectF();
      paint = new Paint();
      rT = new Rect();
      rL = new Rect();
      rR = new Rect();
      rB = new Rect();
      /*rT.set(xMax/2-50, 0, xMax/2+50, 50);
      rL.set(0, yMax/2-50, 50, yMax/2+50);
      rR.set(xMax-50, yMax/2-50, xMax, yMax/2+50);
      rB.set(xMax/2-50, yMax-50, xMax/2+50, yMax);*/
      Log.d("KuchBhio", " "+T1+" "+T2+" "+T3+" "+T4);
   }
  
   // Called back to draw the view. Also called by invalidate().
   @Override
   protected void onDraw(Canvas canvas) {
      // Draw the ball
      //ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
      
      //canvas.drawOval(ballBounds, paint);
        
      // Update the position of the ball, including collision detection and reaction.
      //update();
  
      // Delay
	   j = 0;
	   this.setBackgroundColor(Color.GRAY);
	   paint.setColor(colorTop);
	   canvas.drawRect(xMax/2-50, 0, xMax/2+50, 50, paint);
	   paint.setColor(colorLeft);
	   canvas.drawRect(0, yMax/2-50, 50, yMax/2+50, paint);
	   paint.setColor(colorRight);
	   canvas.drawRect(xMax-50, yMax/2-50, xMax, yMax/2+50, paint);
	   paint.setColor(colorBottom);
	   canvas.drawRect(xMax/2-50, yMax-50, xMax/2+50, yMax, paint);
	   
	   if(i % T1 == 0){
		   if(colorTop == Color.WHITE)colorTop = Color.BLACK;
		   else colorTop = Color.WHITE;
		   j++;
	   }
	   if(i % T2 == 0){
		   if(colorLeft == Color.WHITE)colorLeft = Color.BLACK;
		   else colorLeft = Color.WHITE;
		   j++;
	   }
	   if(i % T3 == 0){
		   if(colorRight == Color.WHITE)colorRight = Color.BLACK;
		   else colorRight = Color.WHITE;
		   j++;
	   }
	   if(i % T4 == 0){
		   if(colorBottom == Color.WHITE)colorBottom = Color.BLACK;
		   else colorBottom = Color.WHITE;
		   j++;
	   }
      try {  
         Thread.sleep(1);  
      } catch (InterruptedException e) { }
      i++;
      if(j == 4)i = 0;
      if(j > 0)invalidate();  // Force a re-draw
   }
   
   // Detect collision and update the position of the ball.
   /*private void update() {
      // Get new (x,y) position
      ballX += ballSpeedX;
      ballY += ballSpeedY;
      // Detect collision and react
      if (ballX + ballRadius > xMax) {
         ballSpeedX = -ballSpeedX;
         ballX = xMax-ballRadius;
      } else if (ballX - ballRadius < xMin) {
         ballSpeedX = -ballSpeedX;
         ballX = xMin+ballRadius;
      }
      if (ballY + ballRadius > yMax) {
         ballSpeedY = -ballSpeedY;
         ballY = yMax - ballRadius;
      } else if (ballY - ballRadius < yMin) {
         ballSpeedY = -ballSpeedY;
         ballY = yMin + ballRadius;
      }
   }*/
   
   // Called back when the view is first created or its size changes.
   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH) {
      // Set the movement bounds for the ball
      xMax = w-1;
      yMax = h-1;
   }
}